#!/usr/bin/env python
#coding=utf-8
'''
Created on Jul 22, 2010
@author: lxd
'''
import wx
from common.tools import Tools
from thread_code import CodeThread
from thread_email import EmailThread
from thread_register import RegisterThread
import random
import parse

class RegisterDialog(wx.Dialog):
    def __init__(self, inviteUrl):
        wx.Dialog.__init__(self, None, -1, u'小号申请', size=(300, 100))
        self.inviteUrl = inviteUrl
        
        proxy_list = self.loadProxy()
        index = random.randint(0, len(proxy_list) - 1)
        self.proxy = proxy_list[index]
        
        self.lblEmail = wx.StaticText(self, -1, u"登录邮箱:")
        self.txtEmail = wx.TextCtrl(self, -1, "", size=(150, -1))
        self.lblPwd = wx.StaticText(self, -1, u"密码:")
        self.txtPwd = wx.TextCtrl(self, -1, "", size=(150, -1))
        self.lblName = wx.StaticText(self, -1, u"真实姓名:")
        self.txtName = wx.TextCtrl(self, -1, "", size=(150, -1))
        self.lblSex = wx.StaticText(self, -1, u"性别:")
        self.radioSex = wx.RadioBox(self, -1, '', (10, 10), wx.DefaultSize, [u'男', u'女'], 2, wx.RA_SPECIFY_COLS)
        self.lblCode = wx.StaticText(self, -1, u"验证码:")
        self.txtCode = wx.TextCtrl(self, -1, "", size=(150, -1))
        self.txtCode.SetFocus()    
        self.sbCode = wx.StaticBitmap(self, -1, size=(64, 20))
        self.btnGetCode = wx.Button(self, -1, u'看不清楚，重新获取')
        self.btnOK = wx.Button(self, -1, u'确定')
        self.btnOK.SetDefault()
        
        infoSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
        infoSizer.AddGrowableCol(1)
        infoSizer.Add(self.lblEmail, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(self.txtEmail, 0, wx.EXPAND)
        infoSizer.Add(self.lblPwd, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(self.txtPwd, 0, wx.EXPAND)
        infoSizer.Add(self.lblName, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(self.txtName, 0, wx.EXPAND)
        infoSizer.Add(self.lblSex, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(self.radioSex, 0, wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(self.lblCode, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(self.txtCode, 0, wx.EXPAND)
        infoSizer.Add((10, 10))
        infoSizer.Add(self.sbCode, 0, wx.ALL)
        infoSizer.Add((10, 10))
        infoSizer.Add(self.btnGetCode, 0, wx.ALL)
        
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(infoSizer, 0, wx.ALL, 5)
        mainSizer.Add(self.btnOK, 0, wx.ALL, 25)
        
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self)  
        self.initValue()
        self.getCode()
        
        self.Bind(wx.EVT_BUTTON, self.OnGetCodeClick, self.btnGetCode) 
        self.Bind(wx.EVT_BUTTON, self.OnOKClick, self.btnOK) 
   
    def initValue(self): 
        self.txtEmail.SetValue(Tools.getRandomEmail(Tools.getRandomWord()))
        self.txtPwd.SetValue(Tools.getRandomNum())
        self.txtName.SetValue(Tools.getRandomName())
        self.radioSex.SetSelection(Tools.getRandomSex() - 1)

    def getCode(self): 
        self.txtCode.Clear()   
        codeThread = CodeThread(self, self.proxy, self.inviteUrl)
        codeThread.setDaemon(True)  
        codeThread.start()  
                
    def OnGetCodeClick(self, event):
        self.getCode()
  
    def loadProxy(self):
        proxy = Tools.loadData(r'db/proxy')
        if not proxy:
            wx.MessageBox(u'请先设置代理', u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION)
        return proxy
        
    def OnOKClick(self, event): 
        if (self.txtEmail.GetValue().strip() != '') \
         and (self.txtPwd.GetValue().strip() != '') \
         and (self.txtName.GetValue().strip() != '') \
         and (self.txtCode.GetValue().strip() != ''):
            
            emailThread = EmailThread(self, self.web, self.inviteUrl, self.txtEmail.GetValue().strip())
            emailThread.setDaemon(True)  
            emailThread.start()              
            
        else:
            wx.MessageBox(u'不得为空', u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION) 

    def infoCall(self, msg): 
        wx.MessageBox(msg, u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION) 
    
    def messageCall(self, msg):
        self.lblMessage.SetLabel(msg)
        
    def codeCall(self, f, web):
        image = parse.getImageFromWeb(f)            
        bitmap = wx.BitmapFromImage(image)           
        self.sbCode.SetBitmap(bitmap)
        self.web = web
    
    def emailCall(self):
        self.txtEmail.SetValue(Tools.getRandomEmail(Tools.getRandomWord()))
        self.OnOKClick(None)
        
    def emailSucessCall(self):
        email = self.txtEmail.GetValue()
        pwd = self.txtPwd.GetValue()
        name = self.txtName.GetValue()
        validateCode = self.txtCode.GetValue()
        sex = self.radioSex.GetSelection() + 1#1男2女
        city = Tools.getRandomCity()#数字不方便显示
        info = dict(email=email, pwd=pwd, name=name, sex=sex, city=city, validateCode=validateCode)
                 
        registerThread = RegisterThread(self, self.web, self.inviteUrl, info)
        registerThread.setDaemon(True)  
        registerThread.start()          
        
    def regSucessCall(self):        
        self.EndModal(wx.ID_OK) 
        
    def addUserCall(self, name, pwd):
        index = self.LstrlContent.InsertStringItem(0, name)
        self.LstrlContent.SetStringItem(index, 1, pwd)
                                   
if __name__ == "__main__":
    app = wx.App()
    inviteUrl = {}
    inviteUrl['id'] = '20251n22241n7a34ad9be6'
    inviteUrl['code'] = '1235879858'
    RegisterDialog(inviteUrl).Show()
    app.MainLoop()
